package Reports::StatsReport;

use utf8;
use strict;
use warnings;

my $VERSION = '1.00';
our @EXPORT_OK = qw(gen_stats_report);
use base qw(Exporter);
use Carp qw(croak);


sub gen_stats_report{
	my %data  = %{$_[0]};
	my $extended_report_file = $_[1];
	my $limit_ascore = $_[2];

	my $espectros_totales = 0;
	my $espectros_fosfo_totales = 0;

	my ($pepts_sequest,  $pepts_omssa,  $pepts_phenyx)  = (0, 0, 0);
	my ($fpepts_sequest, $fpepts_omssa, $fpepts_phenyx) = (0, 0, 0);
	my ($pepts_SO,  $pepts_SP,  $pepts_OP,  $pepts_SOP)  = (0, 0, 0, 0);
	my ($fpepts_SO, $fpepts_SP, $fpepts_OP, $fpepts_SOP) = (0, 0, 0, 0);

	my %pepts_nr;
	my %pepts_cons_nr;
	my %fpepts_nr;
	my %fpepts_nr_ascore;

	# unifico fosfo y DH y quito otras PTMs
	foreach my $spectrum(keys %data){
		unless(defined $data{$spectrum}{'consensus_mod'}){
			next;
		}
		if(defined $data{$spectrum}{'ascore_data'}{'peptide'}){
			$data{$spectrum}{'consensus_mod'} = $data{$spectrum}{'ascore_data'}{'peptide'}
		}
		$data{$spectrum}{'consensus_mod'} =~ s/214|737|35//g;
		$data{$spectrum}{'consensus_mod'} =~ s/\(|\,|\)//g;
		$data{$spectrum}{'consensus_mod'} =~ s/S21|S23/s/g;
		$data{$spectrum}{'consensus_mod'} =~ s/T21|T23/t/g;
		$data{$spectrum}{'consensus_mod'} =~ s/Y21/y/g;
	}

	foreach my $spectrum(keys %data){
		#Solo reporto los consenso en el report Global!!!!
		unless(defined $data{$spectrum}{'consensus_mod'}){next;}
		$espectros_totales++;
		my $phosphos = 0;
		my $spec_cons_mod = $data{$spectrum}{'consensus_mod'};
		my $dummy = $spec_cons_mod ;
		while($dummy =~ s/s|t|y/x/){$phosphos++;}

		if($phosphos){
			$espectros_fosfo_totales++;
			my ($a1, $a2, $a3) = (0, 0, 0);
			if(defined $data{$spectrum}{'ascore_data'}){
				if(defined $data{$spectrum}{'ascore_data'}{'ascore'}[0]){
					if($data{$spectrum}{'ascore_data'}{'ascore'}[0] > $limit_ascore){
						$a1 = 1;
					}
				}
				else{
					print $spectrum, "\n";
					$a1 = 6666;
				}
				if(defined $data{$spectrum}{'ascore_data'}{'ascore'}[1]){
					if($data{$spectrum}{'ascore_data'}{'ascore'}[1] > $limit_ascore){
						$a2 = 1;
					}
				}
				if(defined $data{$spectrum}{'ascore_data'}{'ascore'}[2]){
					if($data{$spectrum}{'ascore_data'}{'ascore'}[2] > $limit_ascore){
						$a3 = 1;
					}
				}
				if(defined $fpepts_nr_ascore{$spec_cons_mod}){
					$fpepts_nr_ascore{$spec_cons_mod}[0] += $a1;
					$fpepts_nr_ascore{$spec_cons_mod}[1] += $a2;
					$fpepts_nr_ascore{$spec_cons_mod}[2] += $a3;
				}
				else{
					@{$fpepts_nr_ascore{$spec_cons_mod}} = ($a1, $a2, $a3);
				}
			}
		}

		my ($seq, $oms, $phe) = (0, 0, 0);

		if((defined $data{$spectrum}{'sequest'}{'unmodified_sequence'})and
		   ($data{$spectrum}{'sequest'}{'unmodified_sequence'} eq $data{$spectrum}{'consensus'})){
			$seq = 1;
			$pepts_sequest++;
		}
		if((defined $data{$spectrum}{'omssa'}{'unmodified_sequence'})and
		   ($data{$spectrum}{'omssa'}{'unmodified_sequence'} eq $data{$spectrum}{'consensus'})){
			$oms = 1;
			$pepts_omssa++;
		}
		if ((defined $data{$spectrum}{'phenyx'}{'unmodified_sequence'})and
		   ($data{$spectrum}{'phenyx'}{'unmodified_sequence'} eq $data{$spectrum}{'consensus'})
		   ){
			$phe = 1;
			$pepts_phenyx++;
		}

		if(defined $pepts_cons_nr{$spec_cons_mod}){
			$pepts_cons_nr{$spec_cons_mod}++;
		}
		else{
			$pepts_cons_nr{$spec_cons_mod} = 1;
		}
		if($seq and $oms and $phe){
			$pepts_SO++;
			$pepts_SP++;
			$pepts_OP++;
			$pepts_SOP++;
			if(defined $pepts_nr{$spec_cons_mod}){
				$pepts_nr{$spec_cons_mod}[0]++;
				$pepts_nr{$spec_cons_mod}[1]++;
				$pepts_nr{$spec_cons_mod}[2]++;
			}
			else{
				$pepts_nr{$spec_cons_mod} = [1, 1, 1];
			}
			if ($phosphos){
				$fpepts_sequest++;
				$fpepts_omssa++;
				$fpepts_phenyx++;
				$fpepts_SO++;
				$fpepts_SP++;
				$fpepts_OP++;
				$fpepts_SOP++;
				if(defined $fpepts_nr{$spec_cons_mod}){
					$fpepts_nr{$spec_cons_mod}[0]++;
					$fpepts_nr{$spec_cons_mod}[1]++;
					$fpepts_nr{$spec_cons_mod}[2]++;
				}
				else{
					$fpepts_nr{$spec_cons_mod} = [1, 1, 1];
				}
			}
		}
		elsif($seq and $oms){
			$pepts_SO++;
			if(defined $pepts_nr{$spec_cons_mod}){
				$pepts_nr{$spec_cons_mod}[0]++;
				$pepts_nr{$spec_cons_mod}[1]++;
			}
			else{
				$pepts_nr{$spec_cons_mod} = [1, 1, 0];
			}
			if ($phosphos){
				$fpepts_sequest++;
				$fpepts_omssa++;
				$fpepts_SO++;
				if(defined $fpepts_nr{$spec_cons_mod}){
					$fpepts_nr{$spec_cons_mod}[0]++;
					$fpepts_nr{$spec_cons_mod}[1]++;
				}
				else{
					$fpepts_nr{$spec_cons_mod} = [1, 1, 0];
				}
			}
		}
		elsif($seq and $phe){
			$pepts_SP++;
			if(defined $pepts_nr{$spec_cons_mod}){
				$pepts_nr{$spec_cons_mod}[0]++;
				$pepts_nr{$spec_cons_mod}[2]++;
			}
			else{
				$pepts_nr{$spec_cons_mod} = [1, 0, 1];
			}
			if($phosphos){
				$fpepts_sequest++;
				$fpepts_phenyx++;
				$fpepts_SP++;
				if(defined $fpepts_nr{$spec_cons_mod}){
					$fpepts_nr{$spec_cons_mod}[0]++;
					$fpepts_nr{$spec_cons_mod}[2]++;
				}
				else{
					$fpepts_nr{$spec_cons_mod} = [1, 0, 1];
				}
			}
		}
		elsif($oms and $phe){
			$pepts_OP++;
			if(defined $pepts_nr{$spec_cons_mod}){
				$pepts_nr{$spec_cons_mod}[1]++;
				$pepts_nr{$spec_cons_mod}[2]++;
			}
			else{
				$pepts_nr{$spec_cons_mod} = [0, 1, 1];
			}
			if ($phosphos){
				$fpepts_omssa++;
				$fpepts_phenyx++;
				$fpepts_OP++;
				if(defined $fpepts_nr{$spec_cons_mod}){
					$fpepts_nr{$spec_cons_mod}[1]++;
					$fpepts_nr{$spec_cons_mod}[2]++;
				}
				else{
					$fpepts_nr{$spec_cons_mod} = [0, 1, 1];
				}
			}
		}
	}
	my $stats_file = $extended_report_file;
	$stats_file = (split /\./, $stats_file)[0];
	$stats_file = $stats_file . '_stats.txt';
	open my $STAT, '>', "$stats_file" or croak "unable to open: $!";
	print $STAT "Total consensus spectra:\t", $espectros_totales, "\n";
	print $STAT "sequest peptides:\t", $pepts_sequest, "\n";
	print $STAT "OMSSA peptides:\t", $pepts_omssa, "\n";
	print $STAT "Phenyx peptides:\t", $pepts_phenyx,"\n";
	print $STAT "S+O peptides:\t", $pepts_SO, "\n";
	print $STAT "S+P peptides:\t", $pepts_SP, "\n";
	print $STAT "O+P peptides:\t", $pepts_OP, "\n";
	print $STAT "S+P+O peptides:\t", $pepts_SOP, "\n\n";

	print $STAT "Total phospho consensus spectra:\t", $espectros_fosfo_totales, "\n";
	print $STAT "sequest phospho-peptides:\t", $fpepts_sequest, "\n";
	print $STAT "OMSSA phospho-peptides:\t", $fpepts_omssa, "\n";
	print $STAT "Phenyx phospho-peptides:\t", $fpepts_phenyx, "\n";
	print $STAT "S+O phospho-peptides:\t", $fpepts_SO, "\n";
	print $STAT "S+P phospho-peptides:\t", $fpepts_SP, "\n";
	print $STAT "O+P phospho-peptides:\t", $fpepts_OP, "\n";
	print $STAT "S+P+O phospho-peptides:\t", $fpepts_SOP, "\n\n";

	my $peps_no_red = scalar keys %pepts_nr;
	my $fpeps_no_red = scalar keys %fpepts_nr;

	print $STAT "Non redundant peptides:\t", $peps_no_red, "\n";
	print $STAT "Non redundant phospho-peptides:\t", $fpeps_no_red, "\n\n\n";
	print $STAT "List of non-redundant peptides\t#Spectra\tPhospho\tB\tO\t" .
			   "P\t#Ascore1>lim\t#Ascore2>lim\t#Ascore3>lim\n";

	foreach my $pep (sort(keys(%pepts_nr))) {
		print $STAT $pep, "\t";
		print $STAT $pepts_cons_nr{$pep}, "\t";
		if(defined $fpepts_nr{$pep}){
			print $STAT "1\t";
			print $STAT join("\t", @{$fpepts_nr{$pep}}), "\t";
			print $STAT join("\t", @{$fpepts_nr_ascore{$pep}}), "\t";
		}
		else{
			print $STAT "0\t";
			print $STAT join("\t", @{$pepts_nr{$pep}}), "\t";
			print $STAT ".\t" x 3, "\t";
		}
		print $STAT "\n";
	}
	close $STAT;
	return 1;
}


1;    #262


__END__


=encoding utf8

=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 LIMITATIONS

=head1 SEE ALSO

=cut
